package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.databinding.ActivityBondNewtdBinding
import com.haoshuang.zhouzhoubang.model.bean.BzjBean
import com.haoshuang.zhouzhoubang.model.bean.BzjData
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.SfActivity
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.one.BondTdAdapter5
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.one.BsortAdapter
import com.haoshuang.zhouzhoubang.ui.page.mine.recognizance.pay.preview.PreviewPayDepositActivity
import com.haoshuang.zhouzhoubang.ui.vm.BondNewViewModel9
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import java.math.BigDecimal
import java.text.DecimalFormat


/**
 *门店保证金
 */
class BondTdNewActivity : BaseVMActivity<BondNewViewModel9, ActivityBondNewtdBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "保证金"
    }

    var list = arrayListOf<BzjData.PayCategory>()


    lateinit var adapter: BondTdAdapter5
    lateinit var adapter2: BsortAdapter
    var list2 = arrayListOf<BzjBean.DepositMenu>()

    override fun initData() {
        var  bean=intent.getSerializableExtra("bean") as BzjData.PayCategory
        var bd =  BigDecimal(bean.team_money)
        var  value = bd.setScale(2, BigDecimal.ROUND_HALF_UP)
        binding.tvPrice.setText("¥${value}")
        list.add(bean)

        vm.getData2()
        vm.getData1()
        binding.apply {
            tvBj.clickWithTrigger {
                if (checkbox.isChecked) {
                    startPage<PreviewPayDepositActivity>("team_id" to team_id,"bean" to bean)

                } else {
                   toast("请勾选协议")
                }

            }
            tvXy.clickWithTrigger {
                var url = "${URLConstants.baseUrl}/index/index/agreement?id=9"
                startPage<HelpDetailActivity>("title" to "师傅保证金协议", "url" to url)
            }


            ivHead.clickWithTrigger {
                startPage<SfActivity>("url" to url)
            }
            rvList2.layoutManager = GridLayoutManager(this@BondTdNewActivity, 5)
            rvList.layoutManager = LinearLayoutManager(this@BondTdNewActivity)
            adapter = BondTdAdapter5(list)
            adapter2 = BsortAdapter(list2)
            rvList.adapter = adapter
            rvList2.adapter = adapter2
            tvSh.clickWithTrigger {
                finish()
            }
        }
    }

    var url = ""
    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                list2.addAll(it?.deposit_menu!!)
                adapter2.notifyDataSetChanged()
                binding.tvWeb.text = it.deposit_agree
                if (!it.deposit_money_pic.isNullOrBlank()){
                    url = it?.deposit_money_pic+""
                }


            }
        }
        vm.getData1.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    list.clear()
                    var order_amount = 0.0
                    for (b in it.not_pay_category) {
                        order_amount += b.team_money.toDouble()
                    }
                    val df = DecimalFormat("#0.00")
                    var order_amount2 = df.format(order_amount)
                   // binding.tvPrice.setText("¥${order_amount2}")
                  //  list.addAll(it.not_pay_category)
                   // adapter.notifyDataSetChanged()
                }

            }
        }
    }

}